package com.carlife.ui.widget;

import com.carlife.util.Constants;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelXorXfermode;

public class LineChart extends View {
	
	private Paint  mPaint = new Paint();
	private int length ;
	private int array_index = 0 ;
	private int max_rows_for_ticker= 0  ;
	private int index_for_prev_ticker=0  ;
	private int max_close ;
	private int curr_close ;
   
	private int x_max = 310 ;
	private int x_min = 25 ;
	private int y_max = 300 ;
	private int y_min = 55;
	private int x_length = x_max -  x_min ;
	private int y_length = y_max - y_min  ;
    
	private int price_array[] ;
	private Line current_line ;
	private Line[] temp_line_array ;
    
	private int start_index_for_x_axis = 0 ;
	    	
   public LineChart(Context arg0) {
		super(arg0);
	}


   public LineChart(Context context, Line[] line_array)
   {  
	  super(context);
	  mPaint.setColor(Color.BLACK);
	  mPaint.setStrokeWidth(2);
	  mPaint.setXfermode(new PixelXorXfermode(Color.WHITE));
	  mPaint.setAntiAlias(true);
	  mPaint.setSubpixelText(true);

      length = line_array.length ;

      price_array = new int [ length ] ;
      temp_line_array = line_array ;
      while (array_index < length) {
			current_line = line_array[array_index];
			if (current_line == null || current_line.getTitle() == null) {
				break;
			}
			
			try {
				if (current_line.getClose() != null)
					curr_close = (int) Double.parseDouble(current_line.getClose());
				else
					break;
			} catch (NumberFormatException nfe) {
				Log.w(Constants.LOG_TAG, nfe.getMessage());
			}
			
			price_array[array_index] = curr_close;
			if (curr_close > max_close) {
				max_close = curr_close;
			}

			if ((array_index + 1 >= length) || 
				(
						(line_array[array_index + 1] != null) &&
						(current_line.getTitle() != line_array[array_index + 1].getTitle())
				)
			) {
				if (max_rows_for_ticker < (array_index - index_for_prev_ticker)) {
					max_rows_for_ticker = array_index - index_for_prev_ticker;
					if (index_for_prev_ticker != 0)
						start_index_for_x_axis = index_for_prev_ticker + 1;

				}
				index_for_prev_ticker = array_index;
			}

			array_index++;
		}      // End of while
   }


       @Override protected void onDraw(Canvas canvas) {
    	   int count = 0 ;
    	   int prev_x = x_min ;
    	   int prev_y = y_max ;
    	   int curr_x =0 ;
    	   int curr_y = 0 ;
    	   int paint_array_count = 0 ;
    	   int done = 0 ;
    	   int ticker_start_index = 0 ;
    	   String x_display="";
    	   
            canvas.drawColor(Color.WHITE);
 
            
            canvas.drawLine(x_min, y_min, x_min, y_max, mPaint);
            canvas.drawLine(x_min, y_max, x_max, y_max, mPaint);
            //          Add Values to Y-AXIS
            curr_x = x_min ;
            for (count = ( max_close/10 ) ; count < max_close ; count= count+( max_close/ 10 )  )
            {
            curr_y = y_max - (( count * y_length ) / max_close ) ;

            canvas.drawText( "__" , curr_x-5 , curr_y, mPaint );
            canvas.drawText(  String.valueOf( count )  , curr_x-20 , curr_y , mPaint);

            }
            
            // Add Values on X-AXIS 
            curr_y = y_max ;
            paint_array_count = 0 ;
            for (count = 0 ; count < 5 ; count++ )
            {
          
		            curr_x =   x_max - ((x_length / 5) * count ) ;
		              paint_array_count  = start_index_for_x_axis +
		                                   ( count *  max_rows_for_ticker / 5 ) ;
		                                  
		            canvas.drawText( "|" , curr_x , curr_y, mPaint );
		            x_display=temp_line_array[paint_array_count].getDate();
		            // Extract YYYY-MM Value
		            x_display=x_display.substring(0,7);
		            canvas.drawText(  x_display  , curr_x-35 , curr_y+15, mPaint );

            }

            while ( done == 0   )
            {
            // Draw the Graph
            prev_x = x_min ;
            prev_y = y_max ;

            for (count = 0 ; count < array_index ; count++ )
            {
            curr_x = x_max - ( count * x_length / max_rows_for_ticker ) ;
            curr_y = y_max - (price_array[ count+ticker_start_index ] * y_length / max_close ) ;

            if ( count > 0 )
            {
              canvas.drawLine( prev_x, prev_y , curr_x , curr_y, mPaint );
              if ( count+ticker_start_index+1 == array_index )
              {
                  done = 1 ;
                  break ;
              }
              if (  temp_line_array[count+ticker_start_index].getTitle() !=
                    temp_line_array[count+ticker_start_index+1].getTitle() )
              {  
                      ticker_start_index += count+1 ;
                      mPaint.setColor(Color.BLUE);
                      break ;

              }
            }
            else if ( count == 0 )
             canvas.drawText( temp_line_array[count+ticker_start_index].getTitle() , curr_x-20 , curr_y-10, mPaint );

            prev_x = curr_x ;
            prev_y = curr_y ;
            }   // End For
           
            canvas.drawText( String.valueOf( max_close)  ,
                        x_min-5 , y_max - (max_close * y_length / max_close ), mPaint );

            } // End While

            
        } // End OnDraw
 
} // End of class chartit.
